package back.Java代理.动态代理.jdk动态代理.案例2;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

// 实现 InvocationHandler 接口
public class DynamicProxyHandler implements InvocationHandler {
    // 目标对象
    private final Object target;

    public DynamicProxyHandler(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        preRequest();
        Object result = method.invoke(target, args);
        postRequest();
        return result;
    }

    private void preRequest() {
        System.out.println("Proxy: Logging the time of request.");
    }

    private void postRequest() {
        System.out.println("Proxy: Logging the request complete.");
    }
}